package com.fr.report.web.ui;

import com.fr.base.ArrayUtils;
import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.data.Dictionary;
import com.fr.data.Dictionary.MV;
import com.fr.data.condition.Condition;
import com.fr.data.impl.JQNode;
import com.fr.data.impl.JQNode.Node;
import com.fr.data.impl.JQNode.QNode;
import com.fr.data.impl.TreeAttr;
import com.fr.data.impl.TreeNodeAttr;
import com.fr.report.script.Calculator;
import com.fr.report.script.ParameterMapNameSpace;
import com.fr.report.script.TreeLayerNameSpace;
import com.fr.util.Utils;
import com.fr.web.Repository;
import com.fr.web.core.SessionIDInfor;
import com.fr.web.core.WebUtils;
import com.fr.web.core.service.WidgetService;
import com.fr.web.core.service.WidgetService.DependenceNameSpace;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;

public class TreeEditor extends WriteAbleRepeatEditor
{
  protected TreeAttr treeAttr = new TreeAttr(false);
  protected TreeNodeAttr[] treeNodeAttr;
  private List treeNodeList = new ArrayList();

  public String getXType()
  {
    return "tree";
  }

  public boolean isEditor()
  {
    return false;
  }

  public TreeAttr getTreeAttr()
  {
    return this.treeAttr;
  }

  public void setTreeAttr(TreeAttr paramTreeAttr)
  {
    this.treeAttr = paramTreeAttr;
  }

  public TreeNodeAttr[] getTreeNodeAttr()
  {
    return this.treeNodeAttr;
  }

  public void setTreeNodeAttr(TreeNodeAttr[] paramArrayOfTreeNodeAttr)
  {
    this.treeNodeAttr = paramArrayOfTreeNodeAttr;
  }

  public String[] dependence()
  {
    if (this.treeNodeAttr == null)
      return new String[0];
    int i = this.treeNodeAttr.length;
    String[] arrayOfString = new String[0];
    for (int j = 0; j < i; ++j)
    {
      Dictionary localDictionary = this.treeNodeAttr[j].getDictionary();
      arrayOfString = (String[])ArrayUtils.addAll(localDictionary.getDependence(), arrayOfString);
    }
    return arrayOfString;
  }

  public JSONArray createJSONData(SessionIDInfor paramSessionIDInfor, Calculator paramCalculator, HttpServletRequest paramHttpServletRequest)
    throws Exception
  {
    JSONArray localJSONArray = super.createJSONData(paramSessionIDInfor, paramCalculator, paramHttpServletRequest);
    if (getTreeAttr().isAjax())
      localJSONArray = loadAjaxTreeData(paramSessionIDInfor, paramCalculator, paramHttpServletRequest);
    else
      localJSONArray = loadDirectTreeData(paramSessionIDInfor, paramCalculator, paramHttpServletRequest);
    return localJSONArray;
  }

  public JSONObject createJSONConfig(Object paramObject, Repository paramRepository)
    throws JSONException
  {
    JSONObject localJSONObject = super.createJSONConfig(paramObject, paramRepository);
    localJSONObject.put("render", true);
    try
    {
      localJSONObject.put("editable", isEditable()).put("autoMode", true);
    }
    catch (JSONException localJSONException)
    {
      FRContext.getLogger().log(Level.WARNING, localJSONException.getMessage(), localJSONException);
    }
    localJSONObject.put("mode", "remote");
    localJSONObject.put("async", this.treeAttr.isAjax());
    localJSONObject.put("mutiSelection", this.treeAttr.isMultipleSelection());
    if (!(this.treeAttr.isCascade()))
      localJSONObject.put("cascadecheck", false);
    if ((this.treeAttr.isMultipleSelection()) && (!(this.treeAttr.isReturnArray())))
    {
      localJSONObject.put("returnArray", false);
      if (!(StringUtils.isEmpty(this.treeAttr.getDelimiter())))
        localJSONObject.put("delimiter", this.treeAttr.getDelimiter());
      if (!(StringUtils.isEmpty(this.treeAttr.getStartSymbol())))
        localJSONObject.put("startSymbol", this.treeAttr.getStartSymbol());
      if (!(StringUtils.isEmpty(this.treeAttr.getEndSymbol())))
        localJSONObject.put("endSymbol", this.treeAttr.getEndSymbol());
    }
    if (this.treeAttr.getWidth() > 0)
      localJSONObject.put("popupWidth", this.treeAttr.getWidth());
    if (this.treeAttr.getHeight() > 0)
      localJSONObject.put("popupHeight", this.treeAttr.getHeight());
    String[] arrayOfString = dependence();
    if (!(ArrayUtils.isEmpty(arrayOfString)))
      localJSONObject.put("dependence", arrayOfString);
    return localJSONObject;
  }

  private JSONArray loadAjaxTreeData(SessionIDInfor paramSessionIDInfor, Calculator paramCalculator, HttpServletRequest paramHttpServletRequest)
    throws Exception
  {
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "id");
    String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "value");
    int i = 0;
    if (str1 != null)
      i = str1.split("-").length;
    paramCalculator.pushNameSpace(ParameterMapNameSpace.create(paramSessionIDInfor.getParameterMap4Execute()));
    paramCalculator.pushNameSpace(new WidgetService.DependenceNameSpace(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "dependence")));
    if (str2 != null)
    {
      Object localObject = (Map)paramSessionIDInfor.getAttribute("layerMap");
      if (localObject == null)
      {
        localObject = new HashMap();
        paramSessionIDInfor.setAttribute("layerMap", localObject);
      }
      ((Map)localObject).put(i + "", str2);
      TreeLayerNameSpace localTreeLayerNameSpace = new TreeLayerNameSpace((Map)localObject);
      paramCalculator.pushNameSpace(localTreeLayerNameSpace);
    }
    return ((JSONArray)loadAjaxTreeNode(paramSessionIDInfor, str1, i, paramCalculator, paramHttpServletRequest));
  }

  private JSONArray loadAjaxTreeNode(SessionIDInfor paramSessionIDInfor, String paramString, int paramInt, Calculator paramCalculator, HttpServletRequest paramHttpServletRequest)
    throws Exception
  {
    if (getTreeNodeAttr() == null)
      return null;
    TreeNodeAttr localTreeNodeAttr = getTreeNodeAttr()[paramInt];
    ArrayList localArrayList = new ArrayList();
    produceAjaxTreeNode(paramSessionIDInfor, localTreeNodeAttr, paramString, paramInt, localArrayList, paramCalculator, paramHttpServletRequest);
    return JQNode.toJSONArray(localArrayList);
  }

  private void produceAjaxTreeNode(SessionIDInfor paramSessionIDInfor, TreeNodeAttr paramTreeNodeAttr, String paramString, int paramInt, List paramList, Calculator paramCalculator, HttpServletRequest paramHttpServletRequest)
    throws Exception
  {
    paramCalculator.pushNameSpace(ParameterMapNameSpace.create(paramSessionIDInfor.getParameterMap4Execute()));
    paramCalculator.pushNameSpace(new WidgetService.DependenceNameSpace(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "dependence")));
    paramTreeNodeAttr.getDictionary().reset();
    Iterator localIterator = paramTreeNodeAttr.getDictionary().entrys(paramCalculator);
    Condition localCondition = paramTreeNodeAttr.getDictionary().getCondition();
    int i = 1;
    ArrayList localArrayList = new ArrayList();
    while (true)
    {
      Dictionary.MV localMV;
      Object localObject1;
      Object localObject3;
      while (true)
      {
        if (!(localIterator.hasNext()))
          return;
        localMV = (Dictionary.MV)localIterator.next();
        localObject1 = localMV.getModel();
        Object localObject2 = (Map)paramSessionIDInfor.getAttribute("layerMap");
        if (localObject2 == null)
        {
          localObject2 = new HashMap();
          paramSessionIDInfor.setAttribute("layerMap", localObject2);
        }
        ((Map)localObject2).put((paramInt + 1) + "", localObject1);
        localObject3 = localMV.getView();
        if ((localCondition == null) || (localCondition.eval(localObject1, paramCalculator)))
          break;
      }
      String str = null;
      if (paramInt == 0)
        str = "" + i;
      else
        str = paramString + "-" + i;
      if (isRemoveRepeat())
        if (!(localArrayList.contains(localMV)))
        {
          paramList.add(new JQNode(str, WidgetService.toJSONString(localObject1, paramHttpServletRequest), WidgetService.toJSONString(localObject3, paramHttpServletRequest), false));
          localArrayList.add(localMV);
        }
      else
        paramList.add(new JQNode(str, WidgetService.toJSONString(localObject1, paramHttpServletRequest), WidgetService.toJSONString(localObject3, paramHttpServletRequest), false));
      ++i;
    }
  }

  private JSONArray loadDirectTreeData(SessionIDInfor paramSessionIDInfor, Calculator paramCalculator, HttpServletRequest paramHttpServletRequest)
    throws Exception
  {
    Object localObject = new ArrayList();
    paramCalculator.pushNameSpace(ParameterMapNameSpace.create(paramSessionIDInfor.getParameterMap4Execute()));
    paramCalculator.pushNameSpace(new WidgetService.DependenceNameSpace(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "dependence")));
    ArrayList localArrayList = new ArrayList();
    localObject = generatedDirectTreeData(paramSessionIDInfor, paramCalculator, paramHttpServletRequest, (List)localObject, localArrayList);
    return ((JSONArray)JQNode.QNode.createTreeNode((List)localObject));
  }

  private List generatedDirectTreeData(SessionIDInfor paramSessionIDInfor, Calculator paramCalculator, HttpServletRequest paramHttpServletRequest, List paramList1, List paramList2)
    throws Exception
  {
    traversalTreeData(paramSessionIDInfor, paramCalculator, paramHttpServletRequest, Utils.objectToString(new Integer(0)), 0, paramList1, paramList2);
    return paramList1;
  }

  private void traversalTreeData(SessionIDInfor paramSessionIDInfor, Calculator paramCalculator, HttpServletRequest paramHttpServletRequest, String paramString, int paramInt, List paramList1, List paramList2)
    throws Exception
  {
    if (paramInt == this.treeNodeAttr.length)
      return;
    int i = 1;
    Dictionary localDictionary = this.treeNodeAttr[paramInt].getDictionary();
    localDictionary.reset();
    Iterator localIterator = localDictionary.entrys(paramCalculator);
    Condition localCondition = localDictionary.getCondition();
    while (true)
    {
      Dictionary.MV localMV;
      Object localObject1;
      Object localObject3;
      while (true)
      {
        if (!(localIterator.hasNext()))
          return;
        localMV = (Dictionary.MV)localIterator.next();
        localObject1 = localMV.getModel();
        Object localObject2 = (Map)paramSessionIDInfor.getAttribute("layerMap");
        if (localObject2 == null)
        {
          localObject2 = new HashMap();
          paramSessionIDInfor.setAttribute("layerMap", localObject2);
        }
        ((Map)localObject2).put(Utils.objectToString(new Integer(paramInt + 1)), localObject1);
        TreeLayerNameSpace localTreeLayerNameSpace = new TreeLayerNameSpace((Map)localObject2);
        paramCalculator.pushNameSpace(localTreeLayerNameSpace);
        localObject3 = localMV.getView();
        if ((localCondition == null) || (localCondition.eval(localObject1, paramCalculator)))
          break;
      }
      String str = null;
      if (paramInt == 0)
        str = Utils.objectToString(new Integer(i));
      else
        str = paramString + "-" + i;
      if (isRemoveRepeat())
        if (!(paramList2.contains(new JQNode.Node(paramString, localMV))))
        {
          paramList1.add(new JQNode.QNode(str, paramString, WidgetService.toJSONString(localObject1, paramHttpServletRequest), WidgetService.toJSONString(localObject3, paramHttpServletRequest), this.treeAttr.isMultipleSelection(), 0, true));
          paramList2.add(new JQNode.Node(paramString, localMV));
        }
      else
        paramList1.add(new JQNode.QNode(str, paramString, WidgetService.toJSONString(localObject1, paramHttpServletRequest), WidgetService.toJSONString(localObject3, paramHttpServletRequest), this.treeAttr.isMultipleSelection(), 0, true));
      traversalTreeData(paramSessionIDInfor, paramCalculator, paramHttpServletRequest, str, paramInt + 1, paramList1, paramList2);
      ++i;
    }
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      Object localObject;
      String str = paramXMLableReader.getTagName();
      if (str.equals("TreeAttr"))
      {
        localObject = new TreeAttr();
        ((TreeAttr)localObject).readXML(paramXMLableReader);
        setTreeAttr((TreeAttr)localObject);
      }
      else if (str.equals("TreeNodeAttr"))
      {
        localObject = new TreeNodeAttr();
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
        this.treeNodeList.add(localObject);
        setTreeNodeAttr((TreeNodeAttr[])this.treeNodeList.toArray(new TreeNodeAttr[this.treeNodeList.size()]));
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    if (this.treeAttr != null)
      this.treeAttr.writeXML(paramXMLPrintWriter);
    if (this.treeNodeAttr != null)
    {
      int i = 0;
      int j = this.treeNodeAttr.length;
      while (i < j)
      {
        paramXMLPrintWriter.startTAG("TreeNodeAttr");
        this.treeNodeAttr[i].writeXML(paramXMLPrintWriter);
        paramXMLPrintWriter.end();
        ++i;
      }
    }
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof TreeEditor) && (super.equals(paramObject)) && (ComparatorUtils.equals(this.treeAttr, ((TreeEditor)paramObject).treeAttr)) && (ComparatorUtils.equals(this.treeNodeAttr, ((TreeEditor)paramObject).treeNodeAttr)));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    TreeEditor localTreeEditor = (TreeEditor)super.clone();
    if (this.treeAttr != null)
      localTreeEditor.treeAttr = this.treeAttr;
    if (this.treeNodeAttr != null)
    {
      ArrayList localArrayList = new ArrayList();
      int i = 0;
      int j = this.treeNodeAttr.length;
      while (i < j)
      {
        localArrayList.add(this.treeNodeAttr[i]);
        ++i;
      }
      localTreeEditor.treeNodeAttr = ((TreeNodeAttr[])localArrayList.toArray(new TreeNodeAttr[localArrayList.size()]));
    }
    return localTreeEditor;
  }
}